package com.ruoyi.order.mapper;

import com.ruoyi.order.domain.TechnicianItemOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 常规订单Mapper接口
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
public interface TechnicianItemOrderMapper 
{
    /**
     * 查询常规订单
     * 
     * @param id 常规订单主键
     * @return 常规订单
     */
    public TechnicianItemOrder selectTechnicianItemOrderById(Long id);

    /**
     * 查询常规订单列表
     * 
     * @param technicianItemOrder 常规订单
     * @return 常规订单集合
     */
    public List<TechnicianItemOrder> selectTechnicianItemOrderList(TechnicianItemOrder technicianItemOrder);

    /**
     * 新增常规订单
     * 
     * @param technicianItemOrder 常规订单
     * @return 结果
     */
    public int insertTechnicianItemOrder(TechnicianItemOrder technicianItemOrder);

    /**
     * 修改常规订单
     * 
     * @param technicianItemOrder 常规订单
     * @return 结果
     */
    public int updateTechnicianItemOrder(TechnicianItemOrder technicianItemOrder);

    /**
     * 删除常规订单
     * 
     * @param id 常规订单主键
     * @return 结果
     */
    public int deleteTechnicianItemOrderById(Long id);

    /**
     * 批量删除常规订单
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTechnicianItemOrderByIds(Long[] ids);

    List<TechnicianItemOrder> selectList(@Param("technicianId") Long technicianId);

    List<TechnicianItemOrder> selectUpgradeCount(@Param("technicianId")Long technicianId,@Param("orderNo") String orderNo);

    List<TechnicianItemOrder> seleBellOrder(@Param("technicianId")Long technicianId);

    @Select("select technician_name from t_technician where id = #{tid}")
    String selectTName(@Param("tid") Long tid);

    @Select("select technician_tel from t_technician where id = #{tid}")
    String selectTTel(@Param("tid") Long tid);

    @Select("select technician_avatar from t_technician where id = #{tid}")
    String selectTAvatar(@Param("tid") Long tid);

    @Select("select user_nike_name from t_user where id = #{userId}")
    String selectUserName(@Param("userId") Long userId);

    @Select("select user_tel from t_user where id = #{userId}")
    String selectUserTel(@Param("userId") Long userId);

    @Select("select count(*) from t_user_item_order where user_id = #{userId} and order_type = 0")
    Integer selectUserPlaceOrderCount(@Param("userId") Long userId);

    @Select("select count(*) from t_user_item_order where user_id = #{userId} and order_type = 2")
    Integer selectUserUpgradePlaceOrderCount(@Param("userId") Long userId);

    @Select("select count(*) from t_user_item_order where user_id = #{userId} and order_type = 1")
    Integer selectUserBellPlaceOrderCount(@Param("userId") Long userId);

    @Select("SELECT group_concat(u.label_name ORDER BY find_in_set( u.label_name, t.user_label )) labelName FROM `t_user` t INNER JOIN `t_user_label` u ON find_in_set( u.id, t.user_label )> 0 WHERE t.id = #{userId} GROUP BY t.id")
    String selectUserLabeList(@Param("userId") Long userId);

    @Select("select item_revenue from t_technician_consume_detail where order_no = #{orderNo}")
    BigDecimal selectTechnicianItemIncome(@Param("orderNo") String orderNo);

    @Select("select fare_revenue from t_technician_consume_detail where order_no = #{orderNo}")
    BigDecimal selectTechnicianCarIncome(@Param("orderNo") String orderNo);

    @Select("select share_price from t_broker_consume_detail where order_no = #{orderNo}")
    BigDecimal selectBrokerIncome(@Param("orderNo") String orderNo);

    @Select("select share_price from t_salesman_consume_detail where order_no = #{orderNo}")
    BigDecimal selectSalesmanIncome(@Param("orderNo") String orderNo);

    @Select("select share_price from t_channel_consume_detail where order_no = #{orderNo}")
    BigDecimal selectChannelIncome(@Param("orderNo") String orderNo);

    @Select("       SELECT\n" +
            "            tcc.coupon_subtract AS couponSubtract,\n" +
            "        FROM\n" +
            "            t_user_coupon tuc\n" +
            "                JOIN t_coupon_config tcc ON tuc.coupon_id = tcc.id\n" +
            "        WHERE\n" +
            "            tuc.user_id = #{userId}\n" +
            "            and tcc.coupon_scope_application in (0,#{tid})\n" +
            "            and tuc.coupon_status = 'NOT'\n" +
            "        ORDER BY\n" +
            "            tuc.create_time DESC")
    BigDecimal selectCouponPrice(@Param("userCouponId") Long userCouponId);

    @Select(" select broker_name from t_broker where broker_invitation_code = (select technician_invited_code from t_technician where id = #{tid}) ")
    String selectBrokerName(@Param("tid") Long tid);

    @Select(" select channel_name from t_channel_merchants where channel_merchants_invitation_code = (select user_invited_code from t_user where id = #{userId}) ")
    String selectChannelName(@Param("userId") Long userId);

    @Select(" select salesman_name from t_salesman where salesman_invitation_code = (select user_invited_code from t_user where id = #{userId}) ")
    String selectSalesmanName(@Param("userId") Long userId);

    TechnicianItemOrder selectUpgradeOrder(@Param("orderNo") String orderNo);

    List<TechnicianItemOrder> selectBellOrder(@Param("orderNo") String orderNo);

    @Select("select id from t_user where user_nike_name like concat('%', #{userName}, '%')")
    List<Long> seleUserId(@Param("userName") String userName);

    @Select("select technician_order_status from t_refund_order where order_no = #{orderNo}")
    Integer seleRefundOrderStatus(@Param("orderNo") String orderNo);

    @Select("select id from t_technician where t_technician.technician_name like concat('%', #{userName}, '%')")
    List<Long> seleTid(@Param("tName") String tName);


    TechnicianItemOrder selectOne(@Param("orderNo") String orderNo);

    @Update("update t_technician_item_order set  order_status = #{orderStatus} where order_no = #{orderNo}")
    Integer updateOrderStatus(@Param("orderNo") String orderNo,@Param("orderStatus") Integer orderStatus);

    @Update("update t_technician_consume_detail set fare_revenue = #{amount},item_revenue = 0,price_status = 1 where order_no = #{orderNo}")
    Integer updateTechnicianIncome(@Param("orderNo") String orderNo,@Param("amount") BigDecimal amount);

    @Update("update t_user_coupon set coupon_status = 'NOT' where id = #{userCouponId}")
    Integer updateUserCouponStatus(@Param("userCouponId") Long userCouponId);
}
